import axios from '../../axios'
import config from '../../config'

// 页面配置信息
export const getPageConfig = (data) => {
  return axios({
    url: '/agileTemplate/getPageConfig',
    method: 'post',
    data
  })
}


// 数据
export const queryPageData = (data) => {
  return axios({
    url: '/agileTemplate/queryPageData',
    method: 'post',
    data
  }).then(res=>{
    if (res.ok) {
      var resp = {};
      resp['total'] = res.data.total;
      const datas = res.data.datas;
      const fields = res.data.fields;
      const dd = [];
      if(res.data.total > 0){
        for(var j = 0; j < datas.length; j++){
          const d = {}
          for(var i = 0; i < fields.length; i++){
            d[fields[i]] = datas[j][i]
          }
          dd.push(d)
        }
      }
      resp.rows = dd;
      res.data = resp;
    }
    return res;
  })
}

// 保存
export const add = (data) => {
  return axios({
    url: '/agileTemplate/add',
    method: 'post',
    data
  })
}

// 更新
export const update = (data) => {
  return axios({
    url: '/agileTemplate/update',
    method: 'post',
    data
  })
}

// 删除
export const remove = (data) => {
  return axios({
    url: '/agileTemplate/remove',
    method: 'post',
    data
  })
}

// 批量删除
export const batchRemove = (data) => {
  return axios({
    url: '/agileTemplate/batchRemove',
    method: 'post',
    data
  })
}

// 获取明细
export const get = (data) => {
  return axios({
    url: '/agileTemplate/get',
    method: 'post',
    data
  })
}

// 导出Excel
export const exportExcelFile = (data) => {
  var ps = data;
  if(typeof data === 'object'){
    ps = [];
    for(var k in data){
      ps.push(k + "=" + data[k]);
    }
    ps = ps.join("&");
  }
  window.open(config.baseUrl + "/agileTemplate/exportExcelFile?"+ps, '_blank')
}

// 获取上传Excel导入模板的URL
export const getImportTemplateUrl = (data) => {
  var ps = data;
  if(typeof data === 'object'){    
    ps = [];
    for(var k in data){
      ps.push(k + "=" + data[k]);
    }
    ps = ps.join("&");
  }
  return config.baseUrl + "/agileTemplate/downloadExcelTemplate?"+ps;
}
// 获取上传Excel导入的URL
export const getImportExcelUrl = (data) => {
  var ps = data;
  if(typeof data === 'object'){
    ps = [];
    for(var k in data){
      ps.push(k + "=" + data[k]);
    }
    ps = ps.join("&");
  }
  return config.baseUrl + "/agileTemplate/importExcelFile?"+ps;
}

// 查询树数据
export const queryTreeData = (data) => {
  return axios({
    url: '/agileTemplate/queryTreeData',
    method: 'post',
    data
  })
}